package servlet;

import com.alibaba.fastjson.JSON;
import entity.Flower;
import service.FlowerService;
import service.impl.FlowerServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: DELL
 * @Since: 2024/9/29 10:02
 * @Description:
 */
@WebServlet("/FlowerServlet")
public class FlowerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out=resp.getWriter();
        FlowerService flowerService=new FlowerServiceImpl();
        List<Flower> listAll=flowerService.getAll();
        String jsonStr= JSON.toJSONString(listAll);
        out.print(jsonStr);
        String path=req.getContextPath();
        String opr=req.getParameter("opr");
        if ("add".equals(opr)){
            String name=req.getParameter("name");
            String  anothername=req.getParameter("anothername");
            String property=req.getParameter("property");
            double price= Double.parseDouble(req.getParameter("price"));
            String production=req.getParameter("production");
            int addHangshu=flowerService.add(name,anothername,property,price,production);
            ServletContext application=this.getServletContext();
            if (addHangshu>0){
            out.print("<script>alert('添加成功!');location.href='"+path+"/index.jsp?opr=select'</script>");            }
        }

        else if ("update".equals(opr)){
            int id=Integer.parseInt(req.getParameter("id"));
            String name=req.getParameter("name");
            String  anothername=req.getParameter("anothername");
            String property=req.getParameter("property");
            Double price= Double.valueOf(req.getParameter("price"));
            String production=req.getParameter("production");
            int updateHangshu=flowerService.update(id,name,anothername,property,price,production);
            ServletContext application=this.getServletContext();
            if (updateHangshu>0){
                out.print("<script>alert('修改成功!');location.href='"+path+"/index.jsp?opr=select'</script>");
            }
        }

        else if ("delete".equals(opr)){
            int id=Integer.parseInt(req.getParameter("id"));
            int deleteHangshu=flowerService.delete(id);
            ServletContext application=this.getServletContext();
            if (deleteHangshu>0){
                out.print("<script>alert('删除成功!');location.href='"+path+"/index.jsp?opr=select'</script>");
            }
        }
    }
}
